package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    // 查询所有
    @Override
    public PageResponse<NursingLevelVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {

        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectByPage(pageNum, pageSize, name, status);

        return PageResponse.of(page, NursingLevelVo.class);
    }

    // 新增
    @Override
    public void saveLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtil.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevel.setCreateBy(1671403256519078138L);
        int count = nursingLevelMapper.selectCount(nursingLevel.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_INSERT_FAIL_DUPLICATION_NAME);
        }

        BigDecimal number = BigDecimal.valueOf(999999999.99);
        if (nursingLevelDto.getFee().compareTo(number) > 0) {
            throw new RuntimeException("护理费用不能超过999999999.99");
        }


        nursingLevelMapper.insertLevel(nursingLevel);
    }

    // 查询单个
    @Override
    public NursingLevelVo getById(Long id) {
        int count = nursingLevelMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_SELECT_ID_FAIL);
        }
        return nursingLevelMapper.selectById(id);
    }

    // 删除
    @Override
    public int removeById(Long id) {
        int count = nursingLevelMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_SELECT_ID_FAIL);
        }

        // 判断计划是否是启用状态，如果是则不能删除
        int status = nursingLevelMapper.getStatus(id);
        if (status == 1) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_DELETE_FAIL);
        }
        return nursingLevelMapper.deleteById(id);
    }

    // 修改
    @Override
    public void updateById(NursingLevelDto nursingLevelDto) {
        int count = nursingLevelMapper.countById(nursingLevelDto.getId());
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_SELECT_ID_FAIL);
        }

        BigDecimal number = BigDecimal.valueOf(99999999.99);
        if (nursingLevelDto.getFee().compareTo(number) > 0) {
            throw new RuntimeException("护理费用不能超过99999999.99");
        }

        NursingLevel nursingLevel = new NursingLevel();
        BeanUtil.copyProperties(nursingLevelDto, nursingLevel);
        nursingLevelMapper.updateById(nursingLevel);
    }


    // 状态
    @Override
    public void isEnable(Long id, Integer status) {
        int count = nursingLevelMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_SELECT_ID_FAIL);
        }
        nursingLevelMapper.alterStatus(id, status);

    }

    // 查询所有护理等级
    @Override
    public List<NursingLevelVo> selectAll() {
        return nursingLevelMapper.listAll();
    }

    @Override
    public void insertBatch(List<NursingLevelDto> nursingLevelDtos) {
        List<NursingLevel> levels = BeanUtil.copyToList(nursingLevelDtos, NursingLevel.class);
        nursingLevelMapper.insertBatch(levels);
    }

    @Override
    public List<NursingLevel> listAllByPlanIds(List<Long> ids) {
        return nursingLevelMapper.listAllByPlanIds(ids);
    }


}
